package com.design.reggie.config;

import org.apache.catalina.connector.Connector;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Tomcat配置文件
 * 解决springboot项目请求出现非法字符问题
 * java.lang.IllegalArgumentException:Invalid character found in the request target.
 * The valid characters are defined in RFC 7230 and RFC 3986
 *
 * 出现这个错误的原因是：我们在前后台交互的时候往往使用json格式的字段串参数，
 * 其中含有“{}”“[]”这些特舒符号，在高版本的tomcat（8.5以上）中含有这些字符的请求会被拦截
 */
@Configuration
public class TomcatConfig {

    @Bean
    public TomcatServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addConnectorCustomizers((Connector connector) -> {
            connector.setProperty("relaxedPathChars", "\"<>[\\]^`{|}");
            connector.setProperty("relaxedQueryChars", "\"<>[\\]^`{|}");
        });
        return factory;
    }
}
